﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using socialnexus.classes.models;
using socialnexus.classes.pages;

namespace socialnexus.admin.Users.Friends
{
    public partial class Add : ProtectedPage
    {
        SiteUser su;

        protected void Page_Load(object sender, EventArgs e)
        {
            su = getSiteUser();
            lblFirstName.Text = su.FirstName;
            lblLastName.Text = su.LastName;
            imgPictureURL.AlternateText = su.FirstName + " " + su.LastName;
            imgPictureURL.ImageUrl = su.PictureURL;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string conditions = "";

            if (su != null)
            {
                conditions = "SiteUserId <> " + su.SiteUserId.ToString();
                if (su.GetFriendIds().Count > 0)
                {
                    conditions += " AND " +
                        "SiteUserId NOT IN (" + String.Join(",", su.GetFriendIds().ToArray()) + ") ";
                }
            }
            
            rptrUsers.DataSource = SiteUser.Search(txtUserName.Text, conditions);
            rptrUsers.DataBind();
        }

        protected void lbDoAdd_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            HiddenField hf =(HiddenField)lb.Parent.FindControl("hfUserId");

            SiteUser su = SiteUser.GetById(Request.QueryString["UserId"].ToString());
            if (su != null)
            {
                su.AddFriend(hf.Value);
                Response.Redirect("~/admin/Users/Show.aspx?UserId=" + su.SiteUserId.ToString());
                Response.Close();
            }
        }
    }
}
